﻿using UnityEngine;
using System.Collections;

public class TeethManagerScript : MonoBehaviour 
{
    private PersistentManagerScript persistentManager;
    private GameObject score;
    private ToothScript[] teeth;
    private int badTeethLeft = 0;
    public LayerMask clickableLayer;

    public void Start()
    {
        //OnSpawn();
    }

	public void OnSpawn() 
    {
        persistentManager = GameObject.Find("PersistentManager").GetComponent<PersistentManagerScript>();
        score = GameObject.Find("Score");

        teeth = gameObject.GetComponentsInChildren<ToothScript>();
        int badTeethAmount = 0;
        if (3 < teeth.Length/2)
        {
            badTeethAmount = Random.Range(3, (int)(teeth.Length / 2));
        }
        else
        {
            badTeethAmount = Random.Range(0, teeth.Length);
        }
        for (int i = 0; i < badTeethAmount; i++)
        {
            int randomTooth = Random.Range(1, teeth.Length);
            if (teeth[randomTooth].GetToothBadness())
            {
                i--;
            }
            else
            {
                teeth[randomTooth].SetToothBad();
                badTeethLeft++;
            }
        }
	}

    void Update() 
    {
        if (persistentManager.GetComponent<PersistentManagerScript>().GetState() == GameState.game)
        {
            Ray ray = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y));
            RaycastHit hit;
            if (Input.GetMouseButtonDown(0) && transform.parent.GetComponent<FaceMovementScript>().IsEglibleForOperation())
            {
                if (Physics.Raycast(ray, out hit, 20.0f, clickableLayer))
                {
                    if (hit.transform.name == "Tooth")
                    {
                        if (hit.transform.GetComponent<ToothScript>().GetToothBadness() && hit.transform.GetComponent<ToothScript>().GetToothClickable())
                        {
                            AddScore(100);
                            badTeethLeft--;

                            if (badTeethLeft > 0)
                                hit.transform.parent.parent.SendMessage("LookAt", LookDirection.mouse, SendMessageOptions.DontRequireReceiver);

                            else
                            {
                                hit.transform.parent.parent.SendMessage("LookAt", LookDirection.centerClosedMouth, SendMessageOptions.DontRequireReceiver);
                                BackGroundParticleScript.instance.Stop();
                            }

                            hit.transform.GetComponent<ToothScript>().FlingTooth();
                            TeethParticleScript.instance.StartAt(hit.point + new Vector3(0, 0f, 0));
                        }

                        else if (!hit.transform.GetComponent<ToothScript>().GetToothBadness() && hit.transform.GetComponent<ToothScript>().GetToothClickable())
                        {
                            AddScore(-20);
                            hit.transform.parent.parent.SendMessage("LookRandom", SendMessageOptions.DontRequireReceiver);
                            hit.transform.GetComponent<ToothScript>().FlingTooth();
                        }
                    }
                    else if (hit.transform.tag == "Face")
                    {
                        BloodScript.instance.StartAt(hit.point + new Vector3(0, -1.35f, 0));
                        AddScore(-100);
                        hit.transform.SendMessage("LookAt", LookDirection.circle, SendMessageOptions.DontRequireReceiver);
                    }
                }
            }
        }
	}

    public bool NoMoreBadTeeth()
    {
        return badTeethLeft <= 0;
    }

    void AddScore(float score)
    {
        persistentManager.GetComponent<PersistentManagerScript>().AddToScore(score);
        UpdateScoreText();
    }

    void UpdateScoreText()
    {
        score.GetComponent<GUIText>().text = "Score: " + persistentManager.GetComponent<PersistentManagerScript>().GetCurrentScore();
    }
}
